package com.ningxun.rms.signup.service.impl;

import java.util.Date;
import java.util.List;

import com.ningxun.rms.common.annotation.DataScope;
import com.ningxun.rms.common.utils.DateUtils;
import com.ningxun.rms.common.utils.SecurityUtils;
import com.ningxun.rms.common.utils.spring.SpringUtils;
import com.ningxun.rms.domain.busrequire.BusRequire;
import com.ningxun.rms.mapper.BusRequireMapper;
import com.ningxun.rms.signup.domain.BusSignup;
import com.ningxun.rms.signup.mapper.BusSignupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ningxun.rms.signup.mapper.BusSignupApproveMapper;
import com.ningxun.rms.signup.domain.BusSignupApprove;
import com.ningxun.rms.signup.service.IBusSignupApproveService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 企业审核Service业务层处理
 *
 * @author rms
 * @date 2025-07-15
 */
@Service
public class BusSignupApproveServiceImpl implements IBusSignupApproveService
{
    @Autowired
    private BusSignupApproveMapper busSignupApproveMapper;

    @Autowired
    private BusSignupMapper busSignupMapper;

    @Autowired
    private BusRequireMapper busRequireMapper;

    /**
     * 查询企业审核
     *
     * @param
     * @return 企业审核
     */
    @Override
    public BusSignupApprove selectBusSignupApproveBySignupId(Long signupId)
    {
        return busSignupApproveMapper.selectBusSignupApproveBySignupId(signupId);
    }

    /**
     * 查询企业审核列表
     *
     * @param busSignup 报名信息
     * @return 企业审核
     */
    @Override
    @DataScope(deptAlias = "br")
    public List<BusSignup> selectBusSignupApproveList(BusSignup busSignup)
    {
        return busSignupApproveMapper.selectBusSignupApproveList(busSignup);
    }

    /**
     * 新增企业审核
     *
     * @param busSignupApprove 企业审核
     * @return 结果
     */
    @Override
    public int insertBusSignupApprove(BusSignupApprove busSignupApprove)
    {
        return busSignupApproveMapper.insertBusSignupApprove(busSignupApprove);
    }

    /**
     * 修改企业审核
     *
     * @param busSignupApprove 企业审核
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int updateBusSignupApprove(BusSignupApprove busSignupApprove)
    {
        // 获取当前时间
        Date nowDate = DateUtils.getNowDate();
        // 修改报名信息表中的申请状态
        BusSignup busSignup = new BusSignup();
        busSignup.setSignupId(busSignupApprove.getSignupId());
        busSignup.setApproveStatus(busSignupApprove.getApproveStatus());
        busSignup.setUpdateTime(nowDate);
        int signupResult = busSignupMapper.updateBusSignup(busSignup);
        if (signupResult <= 0) {
            throw new RuntimeException("更新报名信息失败，申请状态未修改成功");
        }
        // 修改企业审核表中的申请状态
        Long approverId = SecurityUtils.getUserId();
        busSignupApprove.setApproverId(approverId);
        busSignupApprove.setApproveTime(nowDate);
        int approveResult = busSignupApproveMapper.updateBusSignupApprove(busSignupApprove);
        if (approveResult <= 0) {
            throw new RuntimeException("更新企业审核信息失败，申请状态未修改成功");
        }

        // 修改岗位剩余数量
        // 查询企业审核表和报名信息表中的审核状态
        BusSignupApprove signupApprove = busSignupApproveMapper.selectBusSignupApproveBySignupId(busSignupApprove.getSignupId());
        BusSignup signup = busSignupMapper.selectBusSignupBySignupId(busSignupApprove.getSignupId());

        // 判断是否为空,判断审核状态是否相同
        if (signup != null && signupApprove != null && signupApprove.getApproveStatus().equals(signup.getApproveStatus())) {
            // 申请成功
            if (signup.getApproveStatus() == 1) {
                BusRequire busRequire = busRequireMapper.selectBusRequireByRequireId(signup.getRequireId());
                // 获取quantity需要招聘人数 recruited已招收人数
                Long quantity = busRequire.getQuantity();
                Long recruited = busRequire.getRecruited();
                if (recruited < quantity) {
                    // 更新数据库信息
                    busRequireMapper.updateBusRequireRecruited(busRequire);
                    BusRequire updateBusRequire = busRequireMapper.selectBusRequireByRequireId(signup.getRequireId());
                    postFill(updateBusRequire);
                } else {
                    busRequire.setRequireState(1L);
                    busRequireMapper.updateBusRequire(busRequire);
                    throw new RuntimeException("岗位已招满");
                }
            }
        }
        // 给微信用户发起通知
        return approveResult;
    }

    /**
     * 批量删除企业审核
     *
     * @param approveIds 需要删除的企业审核主键
     * @return 结果
     */
    @Override
    public int deleteBusSignupApproveByApproveIds(Long[] approveIds)
    {
        return busSignupApproveMapper.deleteBusSignupApproveByApproveIds(approveIds);
    }

    /**
     * 删除企业审核信息
     *
     * @param approveId 企业审核主键
     * @return 结果
     */
    @Override
    public int deleteBusSignupApproveByApproveId(Long approveId)
    {
        return busSignupApproveMapper.deleteBusSignupApproveByApproveId(approveId);
    }

    /**
     * 判断岗位是否已满
     * @param busRequire
     * @return
     */
    private void postFill(BusRequire busRequire) {
        // 获取quantity需要招聘人数 recruited已招收人数
        Long quantity = busRequire.getQuantity();
        Long recruited = busRequire.getRecruited();

        if (quantity.equals(recruited)) {
            busRequire.setRequireState(1L);
            busRequireMapper.updateBusRequire(busRequire);
        }
    }
}
